home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Human Interface Toolbox / Sheets / sheets.c < prev   
Encoding:
C/C++ Source or Header  |  2000-10-06  |  6.4 KB  |  182 lines

  1. /*
  2.     File:        Sheets.c    
  3.  
  4.     Contains:    A sample on how to create sheets.
  5.  
  6.     Written by:     Karl Groethe
  7.  
  8.     Copyright:    Copyright © 2000 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.             You may incorporate this Apple sample source code into your program(s) without
  11.             restriction. This Apple sample source code has been provided "AS IS" and the
  12.             responsibility for its operation is yours. You are not permitted to redistribute
  13.             this Apple sample source code as "Apple sample source code" after having made
  14.             changes. If you're going to re-distribute the source, we require that you make
  15.             it clear in the source that the code was descended from Apple sample source
  16.             code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                         6/28/00     created
  20.                         7/27/00        added nib sheet support
  21.                 
  22.  
  23. */
  24. #include <Carbon/Carbon.h>
  25. #include <unistd.h>
  26.  
  27. pascal OSStatus myWindowCloseHandler(    EventHandlerCallRef inRef,
  28.                                                 EventRef inEvent,
  29.                                                 void* userData);
  30. pascal OSStatus mySheetHandler(    EventHandlerCallRef inRef,
  31.                                                 EventRef inEvent,
  32.                                                 void* userData);
  33. void newStdSheetWindow(WindowRef parent);
  34. void newNibSheetWindow(WindowRef parent);
  35.  
  36. // ----------------------------------------------------------------------
  37.  
  38. int main(int argc, char* argv[])
  39. {
  40.     IBNibRef         nibRef;
  41.     WindowRef         StdSheetWindow,NibSheetWindow;
  42.     
  43.     static EventTypeSpec closeEvent={kEventClassWindow,kEventWindowClose};
  44.     
  45.     /*setup interface from nib file*/
  46.     CreateNibReference(CFSTR("Main"), &nibRef);
  47.     SetMenuBarFromNib(nibRef, CFSTR("MainMenu"));
  48.     CreateWindowFromNib(nibRef, CFSTR("StdSheetWindow"), &StdSheetWindow);
  49.     CreateWindowFromNib(nibRef, CFSTR("NibSheetWindow"), &NibSheetWindow);
  50.     DisposeNibReference(nibRef);
  51.     /*install event handler for window to handle close box */
  52.     InstallWindowEventHandler(    StdSheetWindow,
  53.                                 NewEventHandlerUPP(myWindowCloseHandler),
  54.                                 1,&closeEvent,0,NULL);
  55.     InstallWindowEventHandler(    NibSheetWindow,
  56.                                 NewEventHandlerUPP(myWindowCloseHandler),
  57.                                 1,&closeEvent,0,NULL);
  58.  
  59.                                 
  60.     ShowWindow(StdSheetWindow);
  61.     ShowWindow(NibSheetWindow);
  62.     
  63.     RunApplicationEventLoop();
  64.     
  65.     return 0;
  66. }
  67.  
  68. pascal OSStatus myWindowCloseHandler(    EventHandlerCallRef inRef,
  69.                                                 EventRef inEvent,
  70.                                                 void* userData)
  71. {
  72.     /*------------------------------------------------------
  73.         A simple handler for close window events
  74.     --------------------------------------------------------*/
  75.  
  76.     WindowRef window;
  77.     CFStringRef windowTitle;
  78.     
  79.     //get window
  80.     GetEventParameter(inEvent,kEventParamDirectObject,typeWindowRef,NULL,sizeof(WindowRef),NULL,&window);
  81.     //make a sheet for window
  82.     CopyWindowTitleAsCFString(window,&windowTitle);
  83.     if(!CFStringCompare(windowTitle,CFSTR("StdSheetWindow"),0))
  84.         newStdSheetWindow(window);
  85.     else if(!CFStringCompare(windowTitle,CFSTR("NibSheetWindow"),0))
  86.         newNibSheetWindow(window);
  87.     
  88.     return noErr;
  89. }
  90.  
  91. void newStdSheetWindow(WindowRef parent)
  92. {
  93.     /*------------------------------------------------------
  94.         Create a Standard Sheet
  95.     --------------------------------------------------------*/
  96.     DialogRef         sheet=NULL;
  97.     static EventTypeSpec controlEvent={kEventClassControl,kEventControlHit};
  98.  
  99.     AlertStdCFStringAlertParamRec alertParams;
  100.     
  101.     GetStandardAlertDefaultParams(&alertParams,kStdCFStringAlertVersionOne);
  102.     //setup the default button
  103.     alertParams.defaultText=(CFStringRef)kAlertDefaultOKText;    
  104.     alertParams.defaultButton=kAlertStdAlertOKButton;    
  105.     //setup the cancel button    
  106.     alertParams.cancelText=(CFStringRef)kAlertDefaultCancelText;
  107.     alertParams.cancelButton=kAlertStdAlertCancelButton;
  108.     
  109.     CreateStandardSheet(kAlertPlainAlert,
  110.                         CFSTR("This is an example of a Standard Sheet"),
  111.                         CFSTR("(extra text goes here)"),&alertParams,
  112.                         GetWindowEventTarget(parent),&sheet);
  113.                     
  114.     
  115.     //Install event handler for controls in sheet
  116.     InstallWindowEventHandler(GetDialogWindow(sheet),
  117.                                 NewEventHandlerUPP(mySheetHandler),
  118.                                 1,&controlEvent,0,NULL);
  119.                                 
  120.     ShowSheetWindow(GetDialogWindow(sheet),parent);
  121. }
  122.  
  123. void newNibSheetWindow(WindowRef parent)
  124. {
  125.     /*------------------------------------------------------
  126.         Create a sheet from a nib file
  127.     --------------------------------------------------------*/
  128.     static EventTypeSpec controlEvent={kEventClassControl,kEventControlHit};
  129.     
  130.     IBNibRef         nibRef;
  131.     WindowRef        wind=NULL;
  132.     CreateNibReference(CFSTR("Sheet"), &nibRef);
  133.     CreateWindowFromNib(nibRef, CFSTR("Sheet"), &wind);
  134.     DisposeNibReference(nibRef);
  135.     InstallWindowEventHandler(    wind,
  136.                                 NewEventHandlerUPP(mySheetHandler),
  137.                                 1,&controlEvent,0,NULL);
  138.  
  139.     ShowSheetWindow(wind,parent);
  140. }
  141.  
  142.  
  143. pascal OSStatus mySheetHandler(    EventHandlerCallRef inRef,
  144.                                                 EventRef inEvent,
  145.                                                 void* userData)
  146. {
  147.     /*------------------------------------------------------
  148.         Carbon Event handler to handle our sheet window
  149.     --------------------------------------------------------*/
  150.     ControlRef control;
  151.     UInt32 cmd;
  152.     WindowRef sheet;
  153.     WindowRef parent;
  154.     
  155.     //get control hit by event
  156.     GetEventParameter(inEvent,kEventParamDirectObject,typeControlRef,NULL,sizeof(ControlRef),NULL,&control);
  157.     //get the command for that control
  158.     GetControlCommandID(control,&cmd);
  159.     
  160.     sheet=GetControlOwner(control);
  161.     GetSheetWindowParent(sheet,&parent);
  162.    
  163.     //close the sheet no matter what
  164.     HideSheetWindow(sheet);
  165.     DisposeWindow(sheet);
  166.     sleep(1);
  167.     switch(cmd){
  168.         case kHICommandCancel:
  169.                 break;
  170.         case kHICommandOK://ok button clicked so close window
  171.                 HideWindow(parent);
  172.                 DisposeWindow(parent);
  173.                 break;
  174.     }
  175.     return noErr;
  176. }
  177.     
  178.     
  179.     
  180.     
  181.     
  182.